package com.word.android.sdk;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.applovin.sdk.AppLovinMediationProvider;
import com.applovin.sdk.AppLovinPrivacySettings;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkConfiguration;
import com.applovin.sdk.AppLovinSdkInitializationConfiguration;
import com.applovin.sdk.AppLovinSdkSettings;
import com.kwai.network.sdk.KwaiAdSDK;
import com.kwai.network.sdk.api.KwaiInitCallback;
import com.kwai.network.sdk.api.SdkConfig;
import com.kwai.network.sdk.core.KwaiCustomController;
import com.word.android.sdk.ad.WordKwaiReward;
import com.word.android.sdk.ad.WordMaxReward;
import com.word.android.sdk.utils.WordAesUtils;
import com.word.android.sdk.utils.WordSPUtils;

public class SDKManager {

    public static SDKManager getInstance() {
        return InstanceHolder.GAME_APPLICATION;
    }

    private static class InstanceHolder {
        private static final SDKManager GAME_APPLICATION = new SDKManager();
    }


    public void init(Activity activity) {
        Log.e("WTMA", "onCreateonCreate");
        Context mApplication = activity.getApplicationContext();
        ((Application) mApplication).registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {

            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
                WordBusiness.onActivityCreate(activity);
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {

            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {
            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {
                WordBusiness.onActivityDestroyed(activity);
            }
        });

        WordBusiness.init(mApplication);

        //AppLovinPrivacySettings
        AppLovinPrivacySettings.setHasUserConsent(true, mApplication);
        AppLovinPrivacySettings.setDoNotSell(false, mApplication);
        //AppLovinSdkSettings
        AppLovinSdkSettings settings = AppLovinSdk.getInstance(mApplication).getSettings();
        //settings.setVerboseLogging(true);
        settings.setExtraParameter("enable_black_screen_fixes", "true");
//        AppLovinSdk.getInstance( context ).getSettings().setVerboseLogging( true );
        // Create the initialization configuration
        AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder(
                        WordAesUtils.decrypt(Constant.MAX_KEY, Constant.KEY_FLAG), mApplication)
                .setMediationProvider(AppLovinMediationProvider.MAX)
                .build();
        // Initialize the SDK with the configuration
        AppLovinSdk.getInstance(mApplication).initialize(initConfig, new AppLovinSdk.SdkInitializationListener() {
            @Override
            public void onSdkInitialized(final AppLovinSdkConfiguration config) {
//                AppLovinSdk.getInstance(context).showMediationDebugger();
                Log.e("WTMA", "onSdkInitialized");
                WordMaxReward.getInstance().loadRewardAd(mApplication);

                String country = WordSPUtils.getString(Constant.KEY_COUNTRY,"");
                if (config != null && !TextUtils.isEmpty(config.getCountryCode())
                        && TextUtils.isEmpty(country)) {
                    Log.e("WTMA", config.getCountryCode());
                    WordSPUtils.putString(Constant.KEY_COUNTRY,config.getCountryCode());
                }
            }
        });

        KwaiAdSDK.init(mApplication, new SdkConfig.Builder()
                // appId，必填，请联系KSIB_KwaiNetwork@kuaishou.com申请正式AppId
                .appId(Constant.KWAI_APP_ID)
                // token，必填，请联系KSIB_KwaiNetwork@kuaishou.com申请正式token，必填
                .token(WordAesUtils.decrypt(Constant.KWAI_TOKEN, Constant.KEY_FLAG))
                // 非必填，支持debug模式查看日志信息，接入方正式版本需要关闭
                .debug(Constant.KWAI_DEBUG)
                .customController(new KwaiCustomController() {
                    @Override
                    public boolean canReadInstalledPackages() {
                        return false;
                    }
                })
                // sdk初始化回调
                .setInitCallback(new KwaiInitCallback() {
                    @Override
                    public void onSuccess() {
                        // 初始化成功 load
                        Log.d("WTMA", "KwaiAdSDK init success");
                        String country = WordSPUtils.getString(Constant.KEY_COUNTRY,"");
                        if (Constant.COUNTRY_ID.equals(country) || Constant.COUNTRY_BR.equals(country)) {
                            WordKwaiReward.getInstance().loadRewardAd();
                        }
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        // 初始化失败，如果初始化失败，还去尝试获取SDK的能力，会出现异常，请确保初始化成功调用你需要的方法
                        // 错误码详见第6章节
                        Log.d("WTMA", "KwaiAdSDK init fail " + code + ":" + msg);
                    }
                }).build());

    }
}
